<template>
  <div id="role-making">
    <div class="role-title">
      <div class="text">角色创作</div>
      <div class="addRole" @click="addRole()">新建角色</div>
    </div>

    <div class="role-list">
      <div class="role-item" v-for="item in roleList" :key="item.id">
        <div class="avatar">
          <div class="avatar-placeholder"></div>
        </div>
        <div class="role-info">
          <div class="name">{{ item.name }}</div>
          <div class="info-row">
            <span class="label">岗位：</span>
            <span class="value">{{ item.position }}</span>
          </div>
          <div class="info-row">
            <span class="label">年龄：</span>
            <span class="value">{{ item.age }}</span>
          </div>
          <div class="info-row">
            <span class="label">性格：</span>
            <span class="value">{{ item.personality }}</span>
          </div>
          <div class="info-row">
            <span class="label">数字人形象：</span>
            <span class="value">{{ item.shuzirenPic }}</span>
          </div>
          <div class="info-row">
            <span class="label">数字人声音：</span>
            <span class="value">{{ item.shuzirenVoice }}</span>
          </div>
          <div class="info-row">
            <span class="label">经历：</span>
            <span class="value" :title="item.experience">{{
              item.experience
            }}</span>
          </div>
        </div>
        <div class="actions">
          <span class="action-btn edit" @click="editRole(item)">修改</span>
          <span class="action-btn delete" @click="deleteRole(item)">删除</span>
        </div>
      </div>
    </div>

    <!-- 添加/修改角色弹窗 -->
    <el-dialog
      v-model="showAddOrEdit"
      width="650px"
      :close-on-click-modal="false"
      top="5vh"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="110px"
        :rules="rules"
        class="addForm"
      >
        <el-form-item label="名称：" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="岗位：" prop="position">
          <el-input v-model="form.position" />
        </el-form-item>
        <el-form-item label="年龄：" prop="age">
          <el-input v-model="form.age" />
        </el-form-item>
        <el-form-item label="性格：" prop="personality">
          <el-input type="textarea" v-model="form.personality" />
        </el-form-item>
        <el-form-item label="数字人形象：" prop="shuzirenPic">
          <div class="upload-section">
            <el-upload
              class="avatar-uploader"
              action="#"
              :show-file-list="false"
              :auto-upload="false"
              :on-change="handleAvatarChange"
              accept=".png"
            >
              <div class="upload-area">
                <img
                  v-if="form.shuzirenPic"
                  :src="form.shuzirenPic"
                  class="avatar-preview"
                />
                <div v-else class="upload-placeholder">
                  <el-icon>
                    <Plus />
                  </el-icon>
                </div>
              </div>
            </el-upload>
            <div class="upload-tips">
              <p class="tips-text">
                尺寸500*500px，白底人物头像图，png格式，不超过2MB
              </p>
              <div class="example-placeholder">
                <img
                  v-if="exampleImage"
                  :src="exampleImage"
                  class="example-img"
                />
              </div>
              <div class="example-image">
                <span class="example-label">例如</span>
                <img
                  src="@/assets/img/roleMaking/shuziren-example.png"
                  alt=""
                />
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="数字人声音：" prop="shuzirenVoice">
          <el-select v-model="form.shuzirenVoice" placeholder="选择数字人声音">
            <el-option
              v-for="item in shuzirenVoiceOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="经历：" prop="experience" class="jingliInput">
          <el-input type="textarea" v-model="form.experience" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="saveRole()" color="#155EEF"
            >保存</el-button
          >
          <el-button @click="showAddOrEdit = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      roleList: [
        {
          id: 1,
          name: "陈强",
          position: "私人银行高级理财经理",
          age: "35岁",
          personality: "专业严谨、富于钻研、具有密切的市场洞察力",
          shuzirenPic: "商业比较、沟通与信",
          shuzirenVoice: "低沉稳重，富有磁性",
          experience:
            "10年金融行业从业经验，服务过500+高净值客户10年金融行业从业经验，服务过500+高净值客户10年金融行业从业经验，服务过500+高净值客户10年金融行业从业经验，服务过500+高净值客户",
        },
        {
          id: 2,
          name: "陈强",
          position: "私人银行高级理财经理",
          age: "35岁",
          personality: "专业严谨、富于钻研、具有密切的市场洞察力",
          shuzirenPic: "商业比较、沟通与信",
          shuzirenVoice: "低沉稳重，富有磁性",
          experience: "10年金融行业从业经验，服务过500+高净值客户",
        },
        {
          id: 3,
          name: "陈强",
          position: "私人银行高级理财经理",
          age: "35岁",
          personality: "专业严谨、富于钻研、具有密切的市场洞察力",
          shuzirenPic: "商业比较、沟通与信",
          shuzirenVoice: "低沉稳重，富有磁性",
          experience: "10年金融行业从业经验，服务过500+高净值客户",
        },
        {
          id: 4,
          name: "陈强",
          position: "私人银行高级理财经理",
          age: "35岁",
          personality: "专业严谨、富于钻研、具有密切的市场洞察力",
          shuzirenPic: "商业比较、沟通与信",
          shuzirenVoice: "低沉稳重，富有磁性",
          experience: "10年金融行业从业经验，服务过500+高净值客户",
        },
        {
          id: 5,
          name: "陈强",
          position: "私人银行高级理财经理",
          age: "35岁",
          personality: "专业严谨、富于钻研、具有密切的市场洞察力",
          shuzirenPic: "商业比较、沟通与信",
          shuzirenVoice: "低沉稳重，富有磁性",
          experience: "10年金融行业从业经验，服务过500+高净值客户",
        },
      ],
      shuzirenVoiceOptions: [
        { id: 1, name: "男声1" },
        { id: 2, name: "男声2" },
        { id: 3, name: "男声3" },
      ],
      showAddOrEdit: false,
      exampleImage: "", // 示例图片
      form: {
        name: "",
        position: "",
        age: "",
        personality: "",
        shuzirenPic: "",
        shuzirenVoice: "",
        experience: "",
      },
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        position: [{ required: true, message: "请输入岗位", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        personality: [
          { required: true, message: "请输入性格", trigger: "blur" },
        ],
        shuzirenPic: [
          { required: true, message: "请输入数字人形象", trigger: "change" },
        ],
        shuzirenVoice: [
          { required: true, message: "请输入数字人声音", trigger: "change" },
        ],
        experience: [
          { required: true, message: "请输入经历", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  methods: {
    // 新建角色
    addRole() {
      this.showAddOrEdit = true;
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.form = {};
    },
    // 修改角色
    editRole(item) {
      this.showAddOrEdit = true;
      this.form = item;
    },
    // 删除角色
    deleteRole(item) {
      this.$confirm("确定删除该角色吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {});
    },
    // 保存角色
    saveRole() {
      this.showAddOrEdit = false;
    },
    // 处理头像上传
    handleAvatarChange(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      const isPNG = file.raw.type === "image/png";

      if (!isPNG) {
        this.$message.error("只能上传 PNG 格式的图片！");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB！");
        return false;
      }

      // 创建图片预览
      const reader = new FileReader();
      reader.onload = (e) => {
        this.form.shuzirenPic = e.target.result;
      };
      reader.readAsDataURL(file.raw);
    },
  },
};
</script>
<style lang="scss" scoped>
#role-making {
  width: 1200px;
  margin: 0 auto;

  .role-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    .text {
      font-weight: bold;
      font-size: 24px;
      color: #0a0a0a;
    }
    .addRole {
      padding: 10px 17px;
      background: #4f46e5;
      border-radius: 4px;
      font-size: 16px;
      color: #ffffff;
      text-align: center;
      cursor: pointer;
      &:hover {
        background: #4338ca;
      }
    }
  }

  .role-list {
    display: flex;
    flex-wrap: wrap;
    gap: 30px;

    .role-item {
      width: calc(33% - 20px);
      background: #ffffff;
      box-shadow: 0px 0px 28px 1px rgba(0, 0, 0, 0.08);
      border-radius: 20px;
      padding: 35px 25px 20px 25px;
      transition: all 0.3s ease;
      display: flex;
      flex-direction: column;
      align-items: center;
      box-sizing: border-box;

      &:hover {
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
        transform: translateY(-2px);
      }

      .avatar {
        margin-bottom: 20px;

        .avatar-placeholder {
          width: 140px;
          height: 140px;
          border-radius: 50%;
          background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
          border: 3px solid #f0f9ff;
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
        }
      }

      .role-info {
        width: 100%;
        margin-bottom: 20px;

        .name {
          font-weight: bold;
          font-size: 21px;
          color: #374151;
          margin-bottom: 15px;
          text-align: left;
        }

        .info-row {
          display: flex;
          align-items: flex-start;
          margin-bottom: 10px;
          font-size: 14px;
          line-height: 1.6;

          &:last-child {
            margin-bottom: 0;
          }

          .label {
            font-size: 14px;
            color: #374151;
            font-weight: bold;
            flex-shrink: 0;
          }

          .value {
            font-size: 14px;
            color: #374151;
            word-break: break-all;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            max-width: 100%;
          }
        }
      }

      .actions {
        width: 100%;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        gap: 20px;
        padding-top: 15px;

        .action-btn {
          font-size: 14px;
          cursor: pointer;
          transition: all 0.2s ease;

          &.edit {
            color: #4f46e5;
          }

          &.delete {
            color: #4f46e5;
          }
        }
      }
    }
  }
  .el-dialog {
    .jingliInput {
      :deep(.el-textarea__inner) {
        min-height: 150px !important;
      }
    }
    .upload-section {
      .avatar-uploader {
        .upload-placeholder {
          width: 130px;
          height: 130px;
          background-color: #fbfdff;
          border: 1px dashed #999;
          border-radius: 50%;
          display: flex;
          justify-content: center;
          align-items: center;
          border-radius: 10px;
          .el-icon {
            font-size: 24px;
            color: #999;
          }
        }
      }
      .upload-area {
        .avatar-preview {
          width: 130px;
          height: 130px;
          object-fit: cover;
        }
      }
      .upload-tips {
        font-size: 14px;
        color: #a0a0a0;
        .example-image {
          display: flex;
          align-items: flex-start;
        }
      }
    }
  }
}
</style>
